//
//  WeichatManager.m
//  IntegralWall
//
//  Created by 小焘无量 on 2022/9/4.
//

#import "WeichatManager.h"
#import <Foundation/Foundation.h>

@interface WeichatManager ()<WXApiDelegate>

@end

@implementation WeichatManager


+(instancetype) shareInstance{
    
    static WeichatManager* _instance = nil;
    static dispatch_once_t onceToken ;
    dispatch_once(&onceToken, ^{
        _instance = [[self alloc] init] ;
    }) ;
    
    return _instance ;
}


- (void)WeiChatPayWithDic:(NSDictionary *)dict {
    
    PayReq *req   = [[PayReq alloc] init];
    req.nonceStr  = [dict objectForKey:@"noncestr"];//随机串，防重发
    req.timeStamp = [[dict objectForKey:@"timestamp"] intValue];// 时间戳，防重发
    req.package   = [dict objectForKey:@"package"];//商家根据财付通文档填写的数据和签名
    req.partnerId = [dict objectForKey:@"partnerid"];//商家向财付通申请的商家id
    req.prepayId  = [dict objectForKey:@"prepayid"];//预支付订单
    req.sign      = [dict objectForKey:@"sign"];//商家根据微信开放平台文档对数据做的签名
    
    [self hangleWechatPayWith:req];
}

- (void)getWeichatCode{
    if([WXApi isWXAppInstalled]){//判断用户是否已安装微信App
        
        SendAuthReq *req = [[SendAuthReq alloc] init];
        req.state = @"wx_oauth_authorization_state";//用于保持请求和回调的状态，授权请求或原样带回
        req.scope = @"snsapi_userinfo";//授权作用域：获取用户个人信息
        //唤起微信
        [WXApi sendReq:req completion:nil];
    }else{
        //自己简单封装的alert
        [UIAlertController alertWithController:[NSObject getCurrentViewController] title:@"温馨提示" message:@"未安装微信应用或版本过低"];
    }
}

#pragma mark - 微信回调

- (BOOL)handleOpenUrl:(NSURL *)url {
    return [WXApi handleOpenURL:url delegate:self];
}

- (BOOL)handleOpenUniversalLink:(NSUserActivity *)userActivity {
    return [WXApi handleOpenUniversalLink:userActivity delegate:self];
}

- (void)hangleWechatPayWith:(PayReq *)req {
    [WXApi sendReq:req completion:^(BOOL success) {
        if (success) {
            NSLog(@"微信支付成功");
        } else {
             NSLog(@"微信支付异常");
        }
    }];
}
#pragma mark - 微信回调

- (void)onResp:(BaseResp *)resp {
    if ([resp isKindOfClass:[PayResp class]]) {
        /*
         enum  WXErrCode {
         WXSuccess           = 0,    < 成功
         WXErrCodeCommon     = -1,  < 普通错误类型
         WXErrCodeUserCancel = -2,   < 用户点击取消并返回
         WXErrCodeSentFail   = -3,   < 发送失败
         WXErrCodeAuthDeny   = -4,   < 授权失败
         WXErrCodeUnsupport  = -5,   < 微信不支持
         };
         */
        PayResp *response = (PayResp*)resp;
        switch (response.errCode) {
            case WXSuccess: {
                NSLog(@"微信回调支付成功");
                [[NSNotificationCenter defaultCenter] postNotificationName:UserNotificationAlipayOrWechatSuccess object:nil userInfo:nil];
            break;
            }
            case WXErrCodeCommon: {
                NSLog(@"微信回调支付异常");
                break;
            }
            case WXErrCodeUserCancel: {
                NSLog(@"微信回调用户取消支付");
                break;
            }
            case WXErrCodeSentFail: {
                NSLog(@"微信回调发送支付信息失败");
                break;
            }
            case WXErrCodeAuthDeny: {
                NSLog(@"微信回调授权失败");
                break;
            }
            case WXErrCodeUnsupport: {
                NSLog(@"微信回调微信版本暂不支持");
                break;
            }
            default: {
                break;
            }
        }
    }else if ([resp isKindOfClass:[SendAuthResp class]]) {
        SendAuthResp *aresp = (SendAuthResp *)resp;
        NSString *code = aresp.code;
        DLog(@"%@",aresp.code);
        [[NSNotificationCenter defaultCenter] postNotificationName:NotifiNameWeichatCode object:code];
    }else if ([resp isKindOfClass:[SendMessageToWXResp class]]) {
        SendMessageToWXResp *aresp = (SendMessageToWXResp *)resp;
        if (aresp.errCode == 0) {
            DLog(@"分享成功");
//            [[NSNotificationCenter defaultCenter] postNotificationName:NotifiNameShareSuccess object:nil];
        }else if (aresp.errCode == -2) {
            DLog(@"用户取消并返回")
        }
    }
}



@end
